/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.uml2.uml.ExtensionPoint;

import com.cea.papyrus.core.directedit.ILabelControler;
import com.cea.papyrus.core.directedit.LabelCellEditorLocator;
import com.cea.papyrus.core.directedit.LabelControler;
import com.cea.papyrus.core.directedit.LabelDirectEditManager;
import com.cea.papyrus.core.directedit.PropertyAccessor;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.usecase.figure.ExtensionPointFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controller for an {@link ExtensionPoint}
 */
public class ExtensionPointEditPart extends GraphNodeWithSemanticEditPart {
	

	/** Manager for editing classname */
	private DirectEditManager manager;

	/**
	 * Creates a new ExtensionPointEditPart.
	 * @param modelManager the model manager
	 * @param graphNode the graph node
	 */
	public ExtensionPointEditPart(GraphNode graphNode, ModelManager modelManager) {
		super(graphNode, modelManager);
	}

	/**
	 * Creates the figure that represents the extension
	 * @return the figure that represents the extension
	 */
	@Override
	protected ExtensionPointFigure createFigure() {
		return new ExtensionPointFigure();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	@Override
	public ExtensionPoint getUmlElement() {
		return (ExtensionPoint) ((Uml1SemanticModelBridge) (getGraphNode()).getSemanticModel()).getElement();
	}

	/**
	 * the direct edit mechanism requires the edit part to handle the direct edit
	 * request.
	 * @param req the request that called the direct edit.
	 */
	public void performDirectEdit(Request req) {
		initLabelEditManager();
		manager.show();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
	 */
	@Override
	public void performRequest(Request request){
		if (RequestConstants.REQ_DIRECT_EDIT.equals(request.getType())) {
			performDirectEdit(request);
		}
	}
	
	/**
	 * Returns the Name Label of the figure.
	 * @return the label displaying the name of the extension point
	 */
	protected Label getNameLabel() {
		IFigure fig = getFigure();

		if (fig instanceof IconFigure) {
			return (Label) fig;
		} else if (fig instanceof ExtensionPointFigure) {
			return (ExtensionPointFigure)fig;
		} else {
			Debug.error(this, "ExtensionPointEditPart::getNameLabel() : Unknown Figure type.", channel);
			return null;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		Debug.debug(this, "refreshvisuals", channel);

		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p, size);
		IFigure fig = getFigure();

		org.eclipse.uml2.uml.ExtensionPoint currentExtensionPoint = getUmlElement();
		com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(
				getUmlElement());

		((ExtensionPointFigure) fig).refreshColors(getGraphNode());
		((ExtensionPointFigure) fig).refreshPresentation(getPresentation(),currentExtensionPoint, getGraphNode());
		
		boolean hasIcon = currentElement.hasIcon();
		boolean hasShape = currentElement.hasShape();
		
		((ExtensionPointFigure) fig).refreshStereotypes(currentElement.getStereotypes(),getPresentation(), hasIcon, hasShape);
		((ExtensionPointFigure) fig).setName(currentExtensionPoint.getName());

		// qualifiedName
		GraphNode gp= (GraphNode) getGraphNode().getContainer();
		((ExtensionPointFigure) fig).refreshQualifiedName(gp, getGraphNode(), getUmlElement());
		
		// display the extension point	
		((ExtensionPointFigure) fig).setVisible(isVisible());
		if (isVisible()){	
			((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
		}
		
	}

	/**
	 * Init the label direct edit manager.
	 * This method should be called when a Direct Edit request is performed.
	 */
	protected void initLabelEditManager() {
		if(manager==null) {
			// locator for the cell editor (on the name label)
			CellEditorLocator locator = new LabelCellEditorLocator(getNameLabel());
	
			// defining how properties are modified in the model
			PropertyAccessor accessor = new PropertyAccessor() {
				public String getPropertyName() {
					return "Name";
				}
	
				public String getValue() {
					if(isElementImport()){
						return getElementImport().getAlias();
					}
					return getUmlElement().getName();
				}
				public void setValue(String value) {
					if(isElementImport()){
						getElementImport().setAlias(value);
					}
					else{getUmlElement().setName(value);}  
				}
			};
			// defines the controler: link between model and view
			ILabelControler labelControler = new LabelControler(getNameLabel(), accessor);
	
			// creates the editing manager.
			manager = new LabelDirectEditManager(this, locator, labelControler);
		}
	}
}